package com.zxx.pattern.factoryPattern.factoryFunc;

import com.zxx.pattern.factoryPattern.factorySample.BMW;

import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * @author zxx on 2020/12/28
 */
public class Test2 {

    private static BMW bmw;

    private static final String[] classes = {
            "com.zxx.pattern.factoryFunc.FactoryBMW320",
            "com.zxx.pattern.factoryFunc.FactoryBMW523"};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择要生产的汽车的型号：");
        System.out.println("2、BMW 523");
        System.out.println("1、BMW 320");
        int type = scanner.nextInt();

        try {
            Class<?> factory = Class.forName(classes[type - 1]);
            Method createBMW = factory.getMethod("createBMW");
            bmw = (BMW) createBMW.invoke(factory.newInstance());

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (bmw != null) {
            bmw.introduce();
        } else {
            System.out.println("没有这个车型！");
        }
        scanner.close();

    }


}
